package com.qigao.mall.web.controller.admin.sys;


import com.qigao.mall.api.SysCompanyUserService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.admin.sys.SysCompanyUserSaveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:luiz
 * @Date: 2018/3/12 14:43
 * @Descripton:
 * @Modify :
 **/
@RestController
@RequestMapping("/admin/sysCompanyUser")
@Api(tags = "Admin公司员工信息", description = "公司员工信息相关api")
public class SysCompanyUserController {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    SysCompanyUserService sysCompanyUserService;


    /**
     * restful api 增删改查
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ApiOperation(value = "保存公司员工信息", notes = "保存公司员工信息")
    @ApiParam(name = "sysCompanySaveVo", value = "保存公司员工信息 sysCompanyUserSaveVo", required = true)
    public Result<String> save(@RequestBody SysCompanyUserSaveVo sysCompanyUserSaveVo, HttpServletRequest request) {
        ValidatorUtil.validateEntity(sysCompanyUserSaveVo);//校验
        try {
            SysCompanyUserSaveDto sysCompanyUserSaveDto = new SysCompanyUserSaveDto();
            BeanUtils.copyProperties(sysCompanyUserSaveVo, sysCompanyUserSaveDto);
            sysCompanyUserSaveDto.setOperNo(RequestUtils.getCurrentUserNo(request));
            return sysCompanyUserService.save(sysCompanyUserSaveDto);
        } catch (Exception e) {
            logger.error("保存公司员工信息异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "删除公司", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "string", paramType = "path")
    public Result<String> delete(@PathVariable("id") String id) {
        try {
            return sysCompanyUserService.delete(id);
        } catch (Exception e) {
            logger.error("删除公司异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    @RequestMapping(value = "/listByUserAndRoleCode/{userNo}/{roleCode}", method = RequestMethod.POST)
    @ApiOperation(value = "获取所有公司信息", notes = "获取所有公司信息")
    public Result<List<SysCompanyUserResultDto>> listByUserAndRoleCode(@PathVariable("userNo")String userNo, @PathVariable("roleCode")String roleCode, HttpServletRequest request) {
        return sysCompanyUserService.listByUserAndRoleCode(userNo,roleCode);
    }



}
